package com.yundashi.updateapputils.util;


import android.util.Log;

import java.io.DataOutputStream;
import java.io.File;

public class ControlUtils {

    //重启
    public static void ala_set_root_cmd() {
        ala_set_cmd("reboot \n");
    }
    public static void ala_set_root_install(String path){
        ala_set_cmd("install "+path);
    }

    //获取root兼容微信root
    private static void ala_set_cmd(String cmd) {
        try {
            String root = "su";
            File suf, alasuf;
            final String kSuSearchPaths[] = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/"};
            for (int i = 0; i < kSuSearchPaths.length; i++) {
                suf = new File(kSuSearchPaths[i] + "su");
                alasuf = new File(kSuSearchPaths[i] + "alasu");
                if (suf != null && suf.exists()) {
                    root = "su";
                    Log.i(ControlUtils.class.getName(), suf + " find root -- " + root);
                } else if (alasuf != null && alasuf.exists()) {
                    root = "alasu";
                    Log.i(ControlUtils.class.getName(), alasuf + " find root -- " + root);
                } else {
                    Log.e(ControlUtils.class.getName(), kSuSearchPaths[i] + " no find root --");
                }
            }

            Process p = Runtime.getRuntime().exec(root);
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            os.writeBytes(String.format(cmd));
            os.writeBytes("\n");
            os.writeBytes("exit\n");
            os.flush();
            os.close();
            Log.i(ControlUtils.class.getName(), "ala_set_su_cmd start cmd :" + cmd);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
